<template>
  <div class="user-info">
    <el-dropdown>
      <span class="el-dropdown-link">
        <el-avatar
          class="avatar"
          size="medium"
          fit="contain"
          src="https://img0.baidu.com/it/u=2016332176,696389807&fm=26&fmt=auto&gp=0.jpg"
        ></el-avatar>
        <span>{{ name }}</span>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item icon="el-icon-user">用户信息</el-dropdown-item>
          <el-dropdown-item icon="el-icon-setting">系统管理</el-dropdown-item>
          <el-dropdown-item icon="el-icon-circle-close" @click="handleExitClick"
            >退出登录</el-dropdown-item
          >
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent } from 'vue'
import { useStore } from '@/store'
import localCache from '@/utils/cache'
import { useRouter } from 'vue-router'

export default defineComponent({
  setup() {
    const store = useStore()
    const router = useRouter()
    const name = computed(() => store.state.login.userInfo.name)
    // 退出登录
    const handleExitClick = () => {
      localCache.removeCache('token')
      router.push('/main')
    }
    return { name, handleExitClick }
  }
})
</script>

<style lang="less" scoped>
.user-info {
  .el-dropdown-link {
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    .avatar {
      margin-right: 6px;
    }
  }
}
</style>
